package com.returnsoft.security.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.security.dto.PermissionDto;
import com.returnsoft.security.eao.PermissionEao;
import com.returnsoft.security.entity.Permission;
import com.returnsoft.security.exception.ConversionExcepcion;
import com.returnsoft.security.exception.EaoExcepcion;
import com.returnsoft.security.exception.SecurityExcepcion;
import com.returnsoft.security.util.Conversion;

/**
 * Session Bean implementation class PermissionBean
 */
@Stateless
@LocalBean
public class PermissionBean implements PermissionInterface,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private PermissionEao eao;
	@EJB
	private Conversion conv;

	public List<PermissionDto> findAll() throws SecurityException {
		try {
			List<Permission> permissions = eao.findAll();
			List<PermissionDto> permissionsDto = new ArrayList<PermissionDto>();
			for (Permission permission : permissions) {
				PermissionDto permissionDto = conv.fromPermission(permission);
				permissionsDto.add(permissionDto);
			}
			return permissionsDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException(e.getMessage());
		}
	}

	/*public List<PermissionDto> findByUser(Integer userId)
			throws SecurityExcepcion {
		try {
			List<Permission> permissions = eao.findByUser(userId);
			List<PermissionDto> permissionsDto = new ArrayList<PermissionDto>();
			for (Permission permission : permissions) {
				PermissionDto permissionDto = conv.fromPermission(permission);
				permissionsDto.add(permissionDto);
			}
			return permissionsDto;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}*/

}
